Skip to content

Refactor DiffBuilder to use tuple paths and simplify operation handling - #197

Merged
stefankoegl merged 4 commits into
masterfrom
claude/kind-turing-0xzvy2
Oct 8, 2026
Merged

stefankoegl merged 4 commits into
masterfrom
claude/kind-turing-0xzvy2

Conversation

@stefankoegl

@stefankoegl stefankoegl commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Summary

This PR refactors the JSON patch diff generation to use tuple-based paths internally instead of string paths, and simplifies operation handling by working with dictionaries instead of operation objects. This enables more robust array index adjustment when generating move operations.

Key Changes

  • Path representation: Changed internal path representation from strings (e.g., '/a/0/b') to tuples (e.g., ('a', 0, 'b')) to distinguish between object keys (strings) and array indices (integers)

  • Operation handling: Replaced PatchOperation subclass instances with plain dictionaries throughout DiffBuilder, simplifying the code and reducing object instantiation overhead

  • Removed methods: Eliminated _on_undo_add(), _on_undo_remove(), _increment_part(), and _decrement_part() methods from operation classes as they are no longer needed with the new approach

  • New path adjustment functions: Added helper functions to properly adjust array indices when operations affect items:

    • _with_item(): Adjusts paths when an item is added
    • _without_item(): Adjusts paths when an item is removed
    • _item_after(): Tracks where an item ends up after operations
    • _shift(): Shifts array indices by an offset
  • Improved move operation generation: Rewrote _item_added() and _item_removed() to use the new _adjust_following() method, which properly adjusts subsequent operations' paths when a move is generated

  • RFC 6902 compliance: Added validation to prevent moves where the destination is inside the source path (RFC 6902 section 4.4)

  • Test updates: Added comprehensive test cases for move operations with complex nested structures and numeric object keys

Implementation Details

The key insight is that by maintaining paths as tuples during diff generation, we can reliably distinguish between array indices that need adjustment and object keys that don't. When a move operation is generated, the code now properly adjusts all following operations' paths to account for the removal and re-insertion of the moved item, ensuring the patch applies correctly to the original document.

https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM

fixes #179

claude added 4 commits October 8, 2026 20:47
When the diff pairs a removal with a later addition of an equal value
(or vice versa) it replaces them with a move, and has to adjust the
locations of all operations in between. That bookkeeping had several
gaps, so make_patch still produced patches that fail to apply or give a
wrong result:

- the moved value's location was only tracked when both the removal
  and the addition were array elements, and only in its last part, so
  shifts of a parent array index were missed
- operations nested inside the array were shifted with the rules for
  inserting and removing at the array itself, and replace operations
  were not shifted at all
- object keys like "0" were shifted like array indices (and could
  raise a TypeError)

The DiffBuilder now keeps locations as tuples of object keys (str) and
array indices (int) and adjusts the operations in between and the
moved value's location with one set of rules. It no longer creates a
move into a child of its 'from' location, which RFC 6902 does not
allow. The _on_undo_* hooks on the operation classes are no longer
needed and are removed.

Out of random array diffs that used to fail about 1.5% of the time,
none fail now, and the generated patches have the same size on average.
The examples for this in property_tests.py pass, so they are removed
from the expected failures, and the "safe" documents now include nested
arrays and numeric object keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
…0xzvy2

# Conflicts:
#	jsonpatch.py
#	property_tests.py
The example from #160 is a value moved from an array into an object
member, which failed for some key orders. The docstring of
test_issue_138 referred to the removed _on_undo methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
…0xzvy2

#173 fixed #160 within the _on_undo bookkeeping that this branch
replaces, so the branch's _item_added and _item_removed are kept. The
duplicate test for #160 is folded into the test_issue_160 from master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
@stefankoegl
stefankoegl merged commit 4b9164d into master Oct 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move-detection bug

2 participants